#ifndef _SRC_MQTT_MSGHANDLER_H_
#define _SRC_MQTT_MSGHANDLER_H_

#include "Entity.h"
#include "MQTTNode.h"

#ifdef __cplusplus
extern "C" {
#endif
/**
 * @brief 处理心跳请求
 * @param entity
 * @return
 */
int32_t handlePingRequest(MQTTEntity *entity);
/**
 * @brief 处理ping response
 * @param entity
 * @param input
 * @param len
 * @return
 */
int32_t handlePingResponse(MQTTEntity *entity, uint8_t *input, uint32_t len);
/**
 * @brief 处理publish
 * @param entity
 * @param input
 * @param len
 * @param qos
 * @param remainLen
 * @return
 */
int32_t handlerPublish(MQTTEntity *entity, uint8_t *input, uint32_t len, uint8_t qos, uint32_t remainLen);
/**
 * @brief 处理publish ack
 * @param entity
 * @param input
 * @param len
 * @return
 */
int32_t handlerPublishAck(MQTTEntity *entity, uint8_t *input, uint32_t len);
/**
 * @brief 处理subscribe 和 subscribe ack 消息
 * @param entity
 * @param input
 * @param len
 * @param packetType
 */
void handlerSubUnsubAck(MQTTEntity *entity, uint8_t *input, uint32_t len, uint8_t packetType);
/**
 * @brief 处理disconnect 消息
 * @param entity
 * @param input
 * @param len
 * @return
 */
int32_t handlerDisconnect(MQTTEntity *entity, uint8_t *input, uint32_t len);
/**
 * @brief 处理 connect ack消息
 * @param entity
 * @param connAck
 * @param remainLen
 * @return
 */
int32_t handleConnectAck(MQTTEntity *entity, uint8_t *connAck, uint32_t remainLen);
/**
 * @brief 通知disconnect 事件
 * @param entity
 * @param disconnect
 */
void notifyDisconnectEvent(MQTTEntity *entity, AiotDisConnectEvent disconnect);
/**
 * @brief 处理连接事件
 * @param entity
 */
void notifyConnectEvent(MQTTEntity *entity);
/**
 * @brief 处理process 数据
 * @param entity
 * @param event
 */
void handleProcessData(MQTTEntity *entity, MQTTEvent *event);
/**
 * @brief 处理topoc alias
 * @param entity
 * @param pubProp
 * @param topicLen
 * @param topic
 * @param aliasNode
 * @return
 */
uint8_t handleTopicAlias(MQTTEntity *entity, PubProperty *pubProp, uint32_t topicLen, char *topic,MQTTTopicAliasNode *aliasNode);
/**
 * @brief 事件通知
 * @param entity
 * @param event
 * @param mqttEvent
 */
void notifyProcessHandler(MQTTEntity *entity, AiotEvent *event, MQTTEvent *mqttEvent);
/**
 * @brief 发送connect 消息
 * @param entity
 * @param prop
 * @return
 */
int32_t handleMqttConnect(MQTTEntity *entity, ConnectProperty *prop);
/**
 * @brief 发送心跳回应包
 * @param entity
 * @return
 */
int32_t sendPingResp(MQTTEntity *entity);
/**
 * @brief 发送public ack
 * @param entity
 * @param packetId
 * @return
 */
int32_t sendPublicAck(MQTTEntity *entity, uint16_t packetId);
/**
 * @brief 读取剩余字节
 * @param entity
 * @param remainlen
 * @param output
 * @return
 */
int32_t readRemainbytes(MQTTEntity *entity, uint32_t remainlen, uint8_t **output);
/**
 * @brief 发送republish
 * @param entity
 * @return
 */
int32_t reSendPublish(MQTTEntity *entity);
/**
 * @brief 发送republish
 * @param entity
 * @return
 */
int32_t reSendSubscribe(MQTTEntity *entity);
/**
 * @brief 发送心跳
 * @param entity
 * @return
 */
int32_t sendHeartbeat(MQTTEntity *entity);
/**
 * @brief 重连
 * @param entity
 * @return
 */
int32_t sendReconnect(MQTTEntity *entity);
/**
 * @brief 读取MQTT 数据
 * @param entity
 * @param buffer
 * @param len
 * @param timeout
 * @return
 */
int32_t readMqttMessage(MQTTEntity *entity, uint8_t *buffer, uint32_t len, int32_t *readLen, uint32_t timeout);
/**
 * @brief 读取剩余长度
 * @param entity
 * @param remainlen
 * @return
 */
int32_t readRemainlen(MQTTEntity *entity, uint32_t *remainlen);
/**
 * @brief 发送MQTT 数据
 * @param entity
 * @param buffer
 * @param len
 * @param timeout
 * @return
 */
int32_t writeMqttData(MQTTEntity *entity, uint8_t *buffer, uint32_t len, int32_t *sendLen, uint32_t timeout);
/**
 * @brief 发送订阅/取消订阅消息
 * @param entity
 * @param topic
 * @param topicLen
 * @param qos
 * @param pktType
 * @param property
 * @return
 */
int32_t sendSubUnsub(MQTTEntity *entity, char *topic, uint16_t topicLen, uint8_t qos, uint8_t pktType,GeneralProperty *property);
/**
 * @brief mqtt tx topic alias 处理
 * @param entity
 * @param topic
 * @param aliasIdPtr
 * @return
 */
int32_t processTxTopicAlias(MQTTEntity *entity, MQTTBuffer *topic, uint16_t *aliasIdPtr);
/**
 * @brief 获取订阅结果
 * @param entity
 * @param params
 * @return
 */
uint8_t sendSubResult(MQTTEntity *entity);

#ifdef __cplusplus
}
#endif
#endif //_SRC_MQTT_MSGHANDLER_H_
